package com.base.cn.platform.os.common.logger;

import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.web.WebUtils;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.stream.IntStream;

/**
 * 专业学习日志对象
 *
 * @author s.li
 * @date 2019/04/25
 */
@Slf4j
public class SubjectStudyLogger {
    /**
     * 生成专业学习日志
     * @param request HttpServletRequest
     * @param subjectIds 访问来源
     */
    public static synchronized void genSubjectStudUvLog(HttpServletRequest request, String subjectIds){
        if(StringUtils.isNotEmpty(subjectIds)){
            subjectIds = StringUtils.subHeadTailString(subjectIds,",");
            String[] arr = subjectIds.split(",");
            if(arr.length > 1){
                String time = DateUtils.format(new Date(),"yyyy-MM-dd");
                String ip = WebUtils.getIpAddr(request);
                IntStream.range(1,arr.length).forEach(index->{
                    String _ids = arr[0]+","+arr[index];
                    StringBuilder loginContext = new StringBuilder();
                    loginContext.append(ip).append("|").append(_ids).append("|").append(time).append("|").append(BigDecimal.ZERO);
                    log.info(loginContext.toString());
                });
            }
        }
    }
}
